package LoginService

import (
	"arabApi/app/api/models/userModel"
	"arabApi/pkg/helpers"
	"crypto/md5"
	"fmt"
	"github.com/spf13/cast"
	rand2 "math/rand"
	"strings"
)

func Login(name string) userModel.User {
	user := userModel.Login(name)
	return user
}
func Login1(user userModel.User) userModel.User {
	user1 := userModel.Login(user.Name)
	return user1
}

func CheckPwd(pwd1, pwd string) bool {
	md5pwd := md5.Sum([]byte(pwd))
	code := fmt.Sprintf("%x", md5pwd)
	if pwd1 == code {
		return true
	}
	return false
}

func GetToken(id uint64) string {

	rand := rand2.Int()
	str := cast.ToString(id) + cast.ToString(rand)
	md5pwd := md5.Sum([]byte(str))
	code := fmt.Sprintf("%x", md5pwd)
	return code
}

func Ser() []userModel.User {
	fmt.Println("tgs", 123)
	user := userModel.Sel()
	return user
}

func UserMsgById(id int64) userModel.User {
	user1 := userModel.UserMsg(id)
	return user1
}

func UserListGetFiled(key string, value string) map[string]string {
	userList := userModel.UserList()
	reList := make(map[string]string, len(userList))
	for _, item := range userList {
		valueMap := helpers.S_struct_to_map(item)
		var keyV = cast.ToString(valueMap[strings.ToLower(key)])
		var valueV = cast.ToString(valueMap[strings.ToLower(value)])
		reList[keyV] = valueV
	}
	return reList
}
